#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from desktop_widget import DesktopWidget
from tray_icon import TrayIcon
from PySide import QtGui
import functools


def singleton(cls, *args, **kwargs):
    @functools.wraps(cls)
    def _singleton(*args, **kwargs):
        import socket
        try:
            # 全局属性，否则变量会在方法退出后被销毁
            global s
            s = socket.socket()
            host = socket.gethostname()
            s.bind((host, 60123))
        except:
            print('already has an instance')
            return None
        return cls(*args, **kwargs)
    return _singleton

@singleton
class Desktop():
    def __init__(self, parent=None):
        self._app = QtGui.QApplication([])
        QtGui.QApplication.setQuitOnLastWindowClosed(False)
        self.window = DesktopWidget(parent)
        self.tray_icon = TrayIcon(self.window)
        self._app.installEventFilter(self.window)

    def show(self):
        self.tray_icon.show_win()
        self._app.exec_()

if __name__ == '__main__':
    aa = Desktop()
    try:
        aa.show()
    except:
        pass